<script setup lang="ts">
import { ref, reactive, onMounted } from 'vue'
import { ElMessage, ElMessageBox } from 'element-plus'
import type { FormInstance, FormRules, UploadProps } from 'element-plus'
import { Plus, Refresh, Search, UploadFilled } from '@element-plus/icons-vue'
import PaginationCommon from '@/components/PaginationCommon.vue'
import UserService from '@/api/services/UserService'
import DepartmentService from '@/api/services/DepartmentService'
import type { User } from '@/types/User'

// 响应数据
const userList = ref<User[]>([])
const loading = ref(false)
const dialogVisible = ref(false)
const dialogTitle = ref('')
const isEdit = ref(false)
const currentUserId = ref<number | null>(null)
const selectedUsers = ref<number[]>([])

// 分页数据
const currentPage = ref(1)
const pageSize = ref(10)
const total = ref(0)


// 表单数据
const form = reactive({
  id: 0,
  account: '',
  password: '',
  realname: '',
  email: '',
  phone: '',
  status: 1 as 1 | 0,
  department: 0, // 部门
  avatarUrl: '' // 头像URL
})

// 表单引用
const formRef = ref<FormInstance>()

// 表单验证规则
const rules = reactive<FormRules>({
  account: [
    { required: true, message: '请输入账号', trigger: 'blur' },
    { min: 3, max: 20, message: '账号长度在 3 到 20 个字符', trigger: 'blur' }
  ],

  realname: [
    { required: true, message: '请输入真实姓名', trigger: 'blur' },
    { min: 2, max: 10, message: '姓名长度在 2 到 10 个字符', trigger: 'blur' }
  ],
  email: [
    { required: true, message: '请输入邮箱地址', trigger: 'blur' },
    { type: 'email', message: '请输入正确的邮箱地址', trigger: 'blur' }
  ],
  phone: [
    { required: true, message: '请输入手机号码', trigger: 'blur' },
    { pattern: /^1[3-9]\d{9}$/, message: '请输入正确的手机号码', trigger: 'blur' }
  ]
})

// 状态选项
const statusOptions = [
  { label: '启用', value: 1 },
  { label: '禁用', value: 0 }
]

// 部门下拉框数据
const departmentOptions = ref<{ id: number; departmentName: string }[]>([])


// 获取用户列表
const fetchUsers = async () => {
  loading.value = true
  try {
    // 调用UserService的getUserPagedListAsync方法
    const result = await UserService.getUserPagedListAsync({
      account: searchForm.account,
      realname: searchForm.realname,
      email: searchForm.email,
      phone: searchForm.phone,
      status: searchForm.status,
      pageIndex: currentPage.value,
      pageSize: pageSize.value
    })

    if (result.success) {
      // 更新用户列表和分页数据
      userList.value = result.data.data
      total.value = result.data.totalCount
      currentPage.value = result.data.pageIndex
      pageSize.value = result.data.pageSize
    } else {
      ElMessage.error(`获取用户列表失败: ${result.message}`)
    }
  } catch (error) {
    console.error('获取用户列表失败:', error)
    ElMessage.error('获取用户列表失败')
  } finally {
    loading.value = false
  }
}

// 分页变化处理
const handlePageChange = (page: number, size: number) => {
  currentPage.value = page
  pageSize.value = size
  fetchUsers()
}

// 打开添加用户对话框
const openAddDialog = () => {
  dialogTitle.value = '添加用户'
  isEdit.value = false
  currentUserId.value = null

  // 重置表单
  Object.assign(form, {
    id: 0,
    account: '',
    realname: '',
    email: '',
    phone: '',
    status: 1
  })

  dialogVisible.value = true
}


// 头像上传前验证和预览
const beforeAvatarUpload: UploadProps['beforeUpload'] = (rawFile) => {
  if (rawFile.type !== 'image/jpeg' && rawFile.type !== 'image/png' && rawFile.type !== 'image/jpg') {
    ElMessage.error('头像必须是 JPG/PNG 格式!')
    return false
  } else if (rawFile.size / 1024 / 1024 > 2) {
    ElMessage.error('头像大小不能超过 2MB!')
    return false
  }

  // 创建本地预览
  const previewUrl = URL.createObjectURL(rawFile)
  form.avatarUrl = previewUrl

  return true
}

// 选择的头像文件
const selectedAvatarFile = ref<File | null>(null)

// 头像上传变化处理 - 用于本地预览
const handleAvatarChange: UploadProps['onChange'] = (file) => {
  if (file.raw) {
    // 文件选择后立即创建本地预览
    const previewUrl = URL.createObjectURL(file.raw)
    form.avatarUrl = previewUrl
    // 保存选择的文件
    selectedAvatarFile.value = file.raw
  }
}

// 获取部门下拉框数据
const fetchDepartmentOptions = async () => {
  try {
    // 调用DepartmentService的getDepartmentDropdownBoxAsync方法，不传入id
    const result = await DepartmentService.getDepartmentDropdownBoxAsync()

    if (result.success) {
      // 更新部门下拉框数据
      departmentOptions.value = result.data as { id: number; departmentName: string }[]
    } else {
      ElMessage.error(`获取部门列表失败: ${result.message}`)
    }
  } catch (error) {
    console.error('获取部门列表失败:', error)
    ElMessage.error('获取部门列表失败')
  }
}

// 打开编辑用户对话框
const openEditDialog = async (user: User) => {
  dialogTitle.value = '编辑用户'
  isEdit.value = true
  currentUserId.value = user.id

  // 获取部门下拉框数据
  await fetchDepartmentOptions()

  // 根据部门名称匹配部门ID
  let departmentId = 0
  if (user.departmentName && user.departmentName !== '无部门') {
    const matchedDept = departmentOptions.value.find(dept => dept.departmentName === user.departmentName)
    departmentId = matchedDept ? matchedDept.id : 0
  }

  // 填充表单数据
  Object.assign(form, {
    id: user.id,
    account: user.account,
    realname: user.realname,
    email: user.email,
    phone: user.phone,
    status: user.status,
    department: departmentId, // 设置部门
    avatarUrl: user.avatarUrl || ''
  })

  dialogVisible.value = true
}

// 提交表单
const submitForm = async () => {
  if (!formRef.value) return
  await formRef.value.validate(async (valid) => {
    if (valid) {
      try {
        if (isEdit.value && form.id) {
          // 查找当前用户以获取userCode
          const currentUser = userList.value.find(user => user.id === form.id)
          const updateParams = {
            id: form.id,
            userCode: currentUser?.userCode || '', // 使用当前用户的userCode
            account: form.account,
            password: '', // 空密码表示不修改
            realname: form.realname,
            phone: form.phone,
            email: form.email,
            status: form.status,
            department: form.department, // 包含部门信息
            avatarUrl: form.avatarUrl || '' // 包含头像URL
          }
          // 更新用户 - 使用新的FormData上传方式
          const result = await UserService.updateUserAsync(updateParams, selectedAvatarFile.value || undefined)

          if (result.success) {
            ElMessage.success('用户更新成功')
            dialogVisible.value = false
            // 重置选择的文件
            selectedAvatarFile.value = null
            fetchUsers() // 刷新列表
          } else {
            ElMessage.error(`更新用户失败: ${result.message}`)
          }
        } else {
          const addParams = {
            account: form.account,
            password: form.password || '', // 使用表单中的密码，如果为空则传空字符串
            realname: form.realname,
          }
          console.log('添加用户参数:', addParams)
          // 添加用户
          const result = await UserService.addUserAsync(addParams)

          if (result.success) {
            ElMessage.success('用户添加成功')
            dialogVisible.value = false
            fetchUsers() // 刷新列表
          } else {
            ElMessage.error(`添加用户失败: ${result.message}`)
          }
        }
      } catch (error) {
        console.error('操作失败:', error)
        ElMessage.error(isEdit.value ? '更新用户失败' : '添加用户失败')
      }
    }
  })
}


// 切换用户状态
const toggleStatus = async (user: User) => {
  try {
    const newStatus = user.status === 1 ? 0 : 1
    const action = newStatus === 1 ? '启用' : '禁用'

    await ElMessageBox.confirm(
      `确定要${action}用户 "${user.realname}" 吗？`,
      `${action}确认`,
      {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning',
      }
    )

    // 调用UserService的单个用户状态更新方法
    const result = await UserService.updateUserStatusAsync(user.id)

    if (result.success) {
      // 更新本地数据状态
      const index = userList.value.findIndex(u => u.id === user.id)
      if (index !== -1) {
        userList.value[index]!.status = newStatus
        userList.value[index]!.updateTime = new Date().toLocaleString('zh-CN')
      }
      ElMessage.success(`用户${action}成功`)
    } else {
      ElMessage.error(`用户${action}失败: ${result.message}`)
    }
  } catch {
    // 用户取消操作
  }
}

// 重置表单
const resetForm = () => {
  if (!formRef.value) return
  formRef.value.resetFields()
}

// 搜索相关数据
const searchForm = reactive({
  account: '',
  realname: '',
  email: '',
  phone: '',
  status: 1 as 1 | 0 | ''
})

// 搜索用户
const searchUsers = () => {
  // 重置到第一页进行搜索
  currentPage.value = 1
  fetchUsers()
}

// 重置搜索
const resetSearch = () => {
  Object.assign(searchForm, {
    account: '',
    realname: '',
    email: '',
    phone: '',
    status: 1
  })
  fetchUsers()
}

// 批量启用用户
const batchEnableUsers = async () => {
  if (selectedUsers.value.length === 0) {
    ElMessage.warning('请选择要启用的用户')
    return
  }

  try {
    const selectedNames = userList.value
      .filter(user => selectedUsers.value.includes(user.id))
      .map(user => user.realname)
      .join('、')

    await ElMessageBox.confirm(
      `确定要启用选中的 ${selectedUsers.value.length} 个用户吗？\n${selectedNames}`,
      '批量启用确认',
      {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning',
      }
    )

    // 调用UserService的批量状态更新方法
    const result = await UserService.updateUsersStatusAsync(selectedUsers.value)

    if (result.success) {
      // 更新本地数据状态
      selectedUsers.value.forEach(userId => {
        const user = userList.value.find(u => u.id === userId)
        if (user) {
          user.status = 1
          user.updateTime = new Date().toLocaleString('zh-CN')
        }
      })
      ElMessage.success('批量启用成功')
      selectedUsers.value = []
    } else {
      ElMessage.error(`批量启用失败: ${result.message}`)
    }
  } catch {
    // 用户取消操作
  }
}

// 批量禁用用户
const batchDisableUsers = async () => {
  if (selectedUsers.value.length === 0) {
    ElMessage.warning('请选择要禁用的用户')
    return
  }

  try {
    const selectedNames = userList.value
      .filter(user => selectedUsers.value.includes(user.id))
      .map(user => user.realname)
      .join('、')

    await ElMessageBox.confirm(
      `确定要禁用选中的 ${selectedUsers.value.length} 个用户吗？\n${selectedNames}`,
      '批量禁用确认',
      {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning',
      }
    )

    // 调用UserService的批量状态更新方法
    const result = await UserService.updateUsersStatusAsync(selectedUsers.value)

    if (result.success) {
      // 更新本地数据状态
      selectedUsers.value.forEach(userId => {
        const user = userList.value.find(u => u.id === userId)
        if (user) {
          user.status = 0
          user.updateTime = new Date().toLocaleString('zh-CN')
        }
      })
      ElMessage.success('批量禁用成功')
      selectedUsers.value = []
    } else {
      ElMessage.error(`批量禁用失败: ${result.message}`)
    }
  } catch {
    // 用户取消操作
  }
}



// 页面加载时获取用户列表
onMounted(() => {
  fetchUsers()
})
</script>

<template>
  <div class="user-container">

    <!-- 搜索栏 - 单行布局 -->
    <el-card class="search-card">
      <div class="search-bar">
        <el-form :model="searchForm" label-width="auto" class="search-form">
          <div class="search-row">
            <el-form-item label="账号">
              <el-input v-model="searchForm.account" placeholder="账号" clearable style="width: 140px" />
            </el-form-item>
            <el-form-item label="姓名">
              <el-input v-model="searchForm.realname" placeholder="姓名" clearable style="width: 140px" />
            </el-form-item>
            <el-form-item label="邮箱">
              <el-input v-model="searchForm.email" placeholder="邮箱" clearable style="width: 160px" />
            </el-form-item>
            <el-form-item label="手机">
              <el-input v-model="searchForm.phone" placeholder="手机" clearable style="width: 150px" />
            </el-form-item>
            <el-form-item label="状态">
              <el-select v-model="searchForm.status" placeholder="状态" clearable style="width: 120px">
                <el-option label="全部" value="" />
                <el-option label="启用" :value="1" />
                <el-option label="禁用" :value="0" />
              </el-select>
            </el-form-item>
            <div class="search-actions">
              <el-button type="primary" @click="searchUsers" size="small">
                <el-icon>
                  <Search />
                </el-icon>
                搜索
              </el-button>
              <el-button @click="resetSearch" size="small">
                <el-icon>
                  <Refresh />
                </el-icon>
                重置
              </el-button>
            </div>
          </div>
        </el-form>
      </div>
    </el-card>

    <!-- 用户列表 -->
    <div class="table-container">
      <el-card class="content-card">
        <template #header>
          <div class="table-header">
            <span>用户列表</span>
            <div class="header-actions">
              <el-button type="primary" @click="openAddDialog">
                <el-icon>
                  <Plus />
                </el-icon>
                添加用户
              </el-button>
              <el-button type="success" @click="batchEnableUsers" :disabled="selectedUsers.length === 0">
                批量启用
              </el-button>
              <el-button type="warning" @click="batchDisableUsers" :disabled="selectedUsers.length === 0">
                批量禁用
              </el-button>
              <el-button @click="fetchUsers">
                <el-icon>
                  <Refresh />
                </el-icon>
                刷新
              </el-button>
            </div>
          </div>
        </template>

        <div class="table-wrapper">
          <el-table :data="userList" v-loading="loading" style="width: 100%" stripe
            @selection-change="(selection: User[]) => selectedUsers = selection.map(user => user.id)">
            <el-table-column type="selection" width="55" fixed="left" />
            <el-table-column prop="id" label="ID" width="80" fixed="left" />
            <el-table-column label="头像" width="80">
              <template #default="{ row }">
                <el-avatar v-if="row.avatarUrl" :src="row.avatarUrl" :size="40" shape="circle" />
                <el-avatar v-else :size="40" shape="circle">
                  {{ row.realname?.charAt(0) || row.account?.charAt(0) || 'U' }}
                </el-avatar>
              </template>
            </el-table-column>
            <el-table-column prop="userCode" label="用户编码" min-width="120" />
            <el-table-column prop="account" label="账号" min-width="120" />
            <el-table-column prop="realname" label="真实姓名" min-width="100" />
            <el-table-column prop="email" label="邮箱" min-width="180" />
            <el-table-column prop="phone" label="手机号" min-width="120" />
            <el-table-column prop="departmentName" label="部门" min-width="120" />
            <el-table-column prop="status" label="状态" width="100">
              <template #default="{ row }">
                <el-tag :type="row.status === 1 ? 'success' : 'danger'">
                  {{ row.status === 1 ? '启用' : '禁用' }}
                </el-tag>
              </template>
            </el-table-column>
            <el-table-column label="操作" width="200" fixed="right">
              <template #default="{ row }">
                <el-button size="small" @click="openEditDialog(row)">
                  编辑
                </el-button>
                <el-button size="small" :type="row.status === 1 ? 'warning' : 'success'" @click="toggleStatus(row)">
                  {{ row.status === 1 ? '禁用' : '启用' }}
                </el-button>
              </template>
            </el-table-column>
          </el-table>
        </div>
      </el-card>

      <!-- 分页组件 - 固定在底部 -->
      <div class="pagination-container">
        <PaginationCommon v-model:current-page="currentPage" v-model:page-size="pageSize" :total="total"
          @page-change="handlePageChange" />
      </div>
    </div>

    <!-- 添加/编辑用户对话框 -->
    <el-dialog v-model="dialogVisible" :title="dialogTitle" width="500px" :close-on-click-modal="false">
      <el-form ref="formRef" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="账号" prop="account">
          <el-input v-model="form.account" placeholder="请输入账号" />
        </el-form-item>

        <el-form-item label="密码" prop="password">
          <el-input v-model="form.password" type="password" placeholder="请输入密码" show-password />
        </el-form-item>

        <el-form-item label="真实姓名" prop="realname">
          <el-input v-model="form.realname" placeholder="请输入真实姓名" />
        </el-form-item>

        <el-form-item v-if="isEdit" label="邮箱" prop="email">
          <el-input v-model="form.email" placeholder="请输入邮箱地址" />
        </el-form-item>

        <el-form-item v-if="isEdit" label="手机号" prop="phone">
          <el-input v-model="form.phone" placeholder="请输入手机号码" />
        </el-form-item>

        <el-form-item v-if="isEdit" label="头像">
          <div class="avatar-upload-container">
            <el-upload class="avatar-uploader" :show-file-list="false" :before-upload="beforeAvatarUpload"
              :on-change="handleAvatarChange" :auto-upload="false">
              <el-avatar v-if="form.avatarUrl" :src="form.avatarUrl" :size="80" shape="circle" />
              <el-icon v-else class="avatar-uploader-icon">
                <UploadFilled />
              </el-icon>
            </el-upload>
            <div class="avatar-tips">
              <p>支持 JPG、PNG 格式，大小不超过 2MB</p>
            </div>
          </div>
        </el-form-item>

        <el-form-item v-if="isEdit" label="部门" prop="department">
          <el-select v-model="form.department" placeholder="请选择部门" clearable style="width: 100%">
            <el-option label="无部门" :value="0" />
            <el-option v-for="dept in departmentOptions" :key="dept.id" :label="dept.departmentName" :value="dept.id" />
          </el-select>
        </el-form-item>

        <el-form-item v-if="isEdit" label="状态" prop="status">
          <el-radio-group v-model="form.status">
            <el-radio v-for="item in statusOptions" :key="item.value" :label="item.value">
              {{ item.label }}
            </el-radio>
          </el-radio-group>
        </el-form-item>
      </el-form>

      <template #footer>
        <span class="dialog-footer">
          <el-button @click="resetForm">重置</el-button>
          <el-button @click="dialogVisible = false">取消</el-button>
          <el-button type="primary" @click="submitForm">
            {{ isEdit ? '更新' : '添加' }}
          </el-button>
        </span>
      </template>
    </el-dialog>
  </div>
</template>

<style scoped>
.user-container {
  padding: 20px;
  height: calc(100vh - 40px);
  /* 减去padding */
  display: flex;
  flex-direction: column;
  box-sizing: border-box;
  overflow: hidden;
  position: relative;
  margin: 0;
  gap: 8px;
  max-height: calc(100vh - 40px);
}

h1 {
  color: #303133;
  margin-bottom: 16px;
  font-size: 24px;
  font-weight: 600;
}

p {
  color: #606266;
  margin-bottom: 24px;
  font-size: 14px;
}

/* 搜索栏样式 - 单行布局 */
.search-card {
  margin-bottom: 0;
  border-radius: 6px;
  box-shadow: 0 1px 4px 0 rgba(0, 0, 0, 0.08);
  flex-shrink: 0;
  overflow: visible;
  z-index: 20;
  position: relative;
  max-height: 80px;
  min-height: 80px;
  background: #fff;
}

.search-bar {
  padding: 12px 20px;
  position: relative;
  z-index: 15;
  background: #fff;
  border-radius: 6px;
  overflow: hidden;
  box-sizing: border-box;
  display: flex;
  flex-direction: column;
  gap: 0;
  height: auto;
}

/* 搜索表单样式 - 单行布局 */
:deep(.search-form) {
  display: flex;
  flex-direction: column;
  gap: 0;
  width: 100%;
}

.search-row {
  display: flex;
  align-items: center;
  gap: 16px;
  width: 100%;
  flex-wrap: nowrap;
  justify-content: space-between;
}

:deep(.search-form .el-form-item) {
  margin-bottom: 0;
  display: flex;
  align-items: center;
  height: 36px;
  margin-right: 0;
  flex-shrink: 0;
}

:deep(.search-form .el-form-item__label) {
  line-height: 36px;
  padding-bottom: 0;
  font-size: 14px;
  margin-right: 8px;
  width: auto;
  display: flex;
  align-items: center;
  color: #606266;
  font-weight: 500;
  min-width: max-content;
  white-space: nowrap;
}

:deep(.search-form .el-input) {
  height: 36px;
  width: auto;
  flex-shrink: 0;
}

:deep(.search-form .el-input__inner) {
  height: 36px;
  line-height: 36px;
  font-size: 14px;
  width: 100%;
  padding: 0 12px;
}

:deep(.search-form .el-select) {
  height: 36px;
  width: auto;
  flex-shrink: 0;
}

:deep(.search-form .el-select .el-input__inner) {
  height: 36px;
  line-height: 36px;
  font-size: 14px;
  width: 100%;
  padding: 0 12px;
}

.search-actions {
  display: flex;
  align-items: center;
  gap: 12px;
  height: 36px;
  margin-left: auto;
  flex-shrink: 0;
}

:deep(.search-actions .el-button) {
  height: 36px;
  padding: 0 16px;
  font-size: 14px;
  min-width: 70px;
}

/* 内联表单样式 */
:deep(.el-form--inline .el-form-item) {
  margin-right: 8px;
  margin-bottom: 0;
  display: inline-flex;
  align-items: center;
}

:deep(.el-form--inline .el-form-item__label) {
  line-height: 28px;
  padding-bottom: 0;
  font-size: 12px;
  margin-right: 4px;
  width: auto;
}

:deep(.el-form--inline .el-input) {
  height: 28px;
  width: auto;
}

:deep(.el-form--inline .el-input__inner) {
  height: 28px;
  line-height: 28px;
  font-size: 12px;
  width: 100%;
}

:deep(.el-form--inline .el-select) {
  height: 28px;
  width: auto;
}

:deep(.el-form--inline .el-select .el-input__inner) {
  height: 28px;
  line-height: 28px;
  font-size: 12px;
  width: 100%;
}

:deep(.el-form--inline .el-button) {
  height: 28px;
  padding: 0 10px;
  font-size: 12px;
  margin-left: 4px;
}

/* 表格容器样式 - 固定高度确保滚动条显示 */
.table-container {
  flex: 1;
  display: flex;
  flex-direction: column;
  min-height: 0;
  overflow: hidden;
  position: relative;
  margin-top: 0;
  height: calc(100vh - 160px);
}

/* 表格包装器样式 - 固定高度和滚动设置 */
.table-wrapper {
  flex: 1;
  min-height: 0;
  overflow: hidden;
  position: relative;
  margin-top: 0;
  height: 100%;
  display: flex;
  flex-direction: column;
}

/* 表格样式 - 固定高度确保滚动条 */
:deep(.el-table) {
  border-radius: 8px;
  overflow: hidden;
  width: 100%;
  flex: 1;
  display: flex;
  flex-direction: column;
  height: 100%;
}

:deep(.el-table .el-table__header-wrapper) {
  flex-shrink: 0;
}

:deep(.el-table .el-table__body-wrapper) {
  flex: 1;
  overflow-y: auto;
  overflow-x: hidden;
  max-height: 470px;
  /* 固定高度确保滚动条 */
}

/* 确保表格行高度合适 */
:deep(.el-table__row) {
  height: 48px;
}

:deep(.el-table .el-table__cell) {
  padding: 8px 0;
}

/* 确保表格行高度合适 */
:deep(.el-table__row) {
  height: 48px;
}

:deep(.el-table .el-table__cell) {
  padding: 8px 0;
}

/* 分页容器样式 - 固定在底部 */
.pagination-container {
  flex-shrink: 0;
  margin-top: 8px;
  padding: 12px 20px;
  background: #fff;
  border-radius: 6px;
  box-shadow: 0 1px 4px 0 rgba(0, 0, 0, 0.08);
  position: sticky;
  bottom: 0;
  z-index: 10;
  max-width: calc(100vw - 40px);
  box-sizing: border-box;
  margin-bottom: 0;
  height: auto;
  min-height: 56px;
  display: flex;
  align-items: center;
  justify-content: center;
  border-top: 1px solid #ebeef5;
}

/* 表格标题样式 - 固定头部 */
.table-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  position: sticky;
  top: 0;
  background: inherit;
  z-index: 10;
}

/* 卡片内容区域样式 - 自适应高度 */
:deep(.el-card__body) {
  padding: 16px;
  margin-top: 0;
  height: auto;
  min-height: 0;
  overflow: visible;
  box-sizing: border-box;
  display: flex;
  flex-direction: column;
}

.header-actions {
  display: flex;
  gap: 12px;
  align-items: center;
}

:deep(.el-table .cell) {
  display: flex;
  align-items: center;
}

:deep(.el-table .el-button) {
  margin-right: 8px;
}

:deep(.el-table .el-button:last-child) {
  margin-right: 0;
}

/* 固定表格头部和操作列 */
:deep(.el-table__header-wrapper) {
  position: sticky;
  top: 0;
  z-index: 10;
}

:deep(.el-table__fixed-header-wrapper) {
  position: sticky;
  top: 0;
  z-index: 11;
}

:deep(.el-table__fixed-right) {
  position: sticky;
  right: 0;
  z-index: 10;
}

:deep(.el-table__fixed-left) {
  position: sticky;
  left: 0;
  z-index: 10;
}

/* 对话框样式 */
:deep(.el-dialog) {
  border-radius: 12px;
}

:deep(.el-dialog__header) {
  border-bottom: 1px solid #ebeef5;
  padding: 20px 20px 10px;
}

:deep(.el-dialog__body) {
  padding: 20px;
}

:deep(.el-dialog__footer) {
  border-top: 1px solid #ebeef5;
  padding: 10px 20px 20px;
}

.dialog-footer {
  display: flex;
  justify-content: flex-end;
  gap: 12px;
}

/* 按钮样式 */
:deep(.el-button) {
  border-radius: 6px;
}

:deep(.el-button--primary) {
  background-color: #409eff;
  border-color: #409eff;
}

:deep(.el-button--primary:hover) {
  background-color: #66b1ff;
  border-color: #66b1ff;
}

/* 输入框样式 */
:deep(.el-input) {
  border-radius: 6px;
}

:deep(.el-input__inner) {
  border-radius: 6px;
}

/* 标签样式 */
:deep(.el-tag) {
  border-radius: 4px;
}

/* 卡片标题样式 */
:deep(.el-card__header) {
  background-color: #f5f7fa;
  border-bottom: 1px solid #ebeef5;
  padding: 16px 20px;
  font-weight: 600;
  color: #303133;
  border-radius: 12px 12px 0 0;
  position: sticky;
  top: 0;
  z-index: 10;
}

/* 头像上传样式 */
.avatar-upload-container {
  display: flex;
  align-items: flex-start;
  gap: 16px;
}

.avatar-uploader {
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
  width: 80px;
  height: 80px;
  display: flex;
  align-items: center;
  justify-content: center;
  background-color: #fafafa;
  transition: border-color 0.3s;
}

.avatar-uploader:hover {
  border-color: #409eff;
}

.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
}

.avatar-tips {
  flex: 1;
}

.avatar-tips p {
  margin: 0;
  font-size: 12px;
  color: #606266;
  line-height: 1.5;
}

.avatar-url {
  word-break: break-all;
  background-color: #f5f7fa;
  padding: 4px 8px;
  border-radius: 4px;
  margin-top: 4px;
  font-family: monospace;
  font-size: 10px;
}
</style>
